package com.letv.android.client.view;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.letv.android.young.client.R;
import com.letv.core.utils.NetworkUtils;
import com.letv.core.utils.TipUtils;

/**
 * 公共加载布局
 * 
 * @author zhuqiao
 *
 */
public class PublicLoadLayout extends RelativeLayout {
    private static final String NO_NET_MSG_KEY = "500011";
    private static final String NO_DATA_MSG_KEY = "500012";
    private static final String LOADING_MSG_KEY = "500008";

    private RelativeLayout frameContent;
    private LinearLayout content;
    private View loading;
    private TextView loadingText;
    private View error;
    private View fullError;
    private Context context;
    private TextView errorTxt1;
    private TextView errorTxt2;
    private TextView fullErrorText;
    private ImageView errorImage;
    private RefreshData mRefreshData;
    // 内容根布局是否是frameLayout，默认是线性布局
    private boolean contentIsFramelayout = false;
    //是否为全屏剧集网络错误提示
    private boolean isFullNetError;
    
    private String mDefaultTextTabNoNetTextUrl;
    private String mDefaultTextTabNoDataTextUrl;
    private String mDefaultTextTabLoadingTextUrl;

    private Handler mHandler;
    // 含有错误及加载
    private View mLoadLayout;

    /**
     * 创建一个公共的加载布局
     */
    public static PublicLoadLayout createPage(Context context, int layoutId) {
        return createPage(context, layoutId, false);
    }

    /**
     * 创建一个公共的加载布局
     * 
     * @param context
     * @param layoutId
     * @param isToFrameLayout 是否将内容添加到FrameLayout中
     * @return
     */
    public static PublicLoadLayout createPage(Context context, int layoutId, boolean isToFrameLayout) {
        PublicLoadLayout rootView = new PublicLoadLayout(context);
        rootView.addContent(layoutId, isToFrameLayout);
        return rootView;
    }

    public PublicLoadLayout(Context context) {
        this(context, null);
    }

    public PublicLoadLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public void setRefreshData(RefreshData refreshData) {
        mRefreshData = refreshData;
    }

    private void init(Context context) {
        this.context = context;
        inflate(context, R.layout.letv_public_loading_layout, this);
        mDefaultTextTabNoNetTextUrl = TipUtils.getTipMessage(context,NO_NET_MSG_KEY, R.string.public_loading_no_net);
        mDefaultTextTabNoDataTextUrl = TipUtils.getTipMessage(context,NO_DATA_MSG_KEY, R.string.public_loading_datanull);
        mDefaultTextTabLoadingTextUrl = TipUtils.getTipMessage(context,LOADING_MSG_KEY, R.string.public_loading_text);
        mHandler = new Handler();
        findView();
        setBackgroundColor(getResources().getColor(R.color.letv_color_fff6f6f6));
    }

    /**
     * 添加内容到指定的LinearLayout中
     * 
     * @param viewId
     */
    public void addContent(int viewId) {
        addContent(viewId, false);
    }

    /**
     * 添加内容到布局中
     * 
     * @param viewId
     * @param toFrameLayout
     * true:内容添加到FrameLayout中,false:添加到LinearLayout中
     */
    public void addContent(int viewId, boolean toFrameLayout) {
        contentIsFramelayout = toFrameLayout;
        if (toFrameLayout) {
            inflate(getContext(), viewId, frameContent);
            frameContent.setVisibility(VISIBLE);
            content.setVisibility(View.GONE);
        } else {
            inflate(getContext(), viewId, content);
            frameContent.setVisibility(View.GONE);
            content.setVisibility(View.VISIBLE);
        }
    }

    public View getContentView() {
        return contentIsFramelayout ? frameContent : content;
    }

    private void findView() {
        errorImage = (ImageView) findViewById(R.id.net_error_flag);
        content = (LinearLayout) findViewById(R.id.content);
        loading = findViewById(R.id.loading_layout);
        loadingText = (TextView) findViewById(R.id.loadingText);
        error = findViewById(R.id.error);
        fullError = findViewById(R.id.full_net_error);
        errorTxt1 = (TextView) findViewById(R.id.errorTxt1);
        errorTxt2 = (TextView) findViewById(R.id.errorTxt2);
        fullErrorText = (TextView) findViewById(R.id.full_episode_play_errer_retry);
        frameContent = (RelativeLayout) findViewById(R.id.frame_content);
        mLoadLayout = findViewById(R.id.public_load_layout);
        // Add all area to be clickable
        error.setOnClickListener(mOnClickListener);
        fullError.setOnClickListener(mOnClickListener);
    }

    final Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            loading.setVisibility(View.GONE);
            if(isFullNetError){
            	fullError.setVisibility(View.VISIBLE);
            }else{
            	error.setVisibility(View.VISIBLE);
            }
            mHandler.removeCallbacks(mRunnable);
        }
    };

    OnClickListener mOnClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!NetworkUtils.isNetworkAvailable(context)) {
                loading.setVisibility(View.VISIBLE);
                fullError.setVisibility(View.GONE);
                error.setVisibility(View.GONE);
                mHandler.postDelayed(mRunnable, 1000);
                return;
            } else {
                if (mRefreshData != null && (error.getVisibility() == View.VISIBLE || fullError.getVisibility() == View.VISIBLE)) {
                    mRefreshData.refreshData();
                    fullError.setVisibility(View.GONE);
                }
                
            }
        }
    };

    public void loading(boolean isShowContent) {
        if (!TextUtils.isEmpty(mDefaultTextTabLoadingTextUrl)) {
            loadingText.setText(mDefaultTextTabLoadingTextUrl);
        } else {
            loadingText.setText(R.string.public_loading_text);
        }
        loading.setVisibility(VISIBLE);
        error.setVisibility(GONE);
        getContentView().setVisibility(isShowContent ? VISIBLE : GONE);
    }

    public void loadingforHot(boolean isShowContent) {
        if (!TextUtils.isEmpty(mDefaultTextTabLoadingTextUrl)) {
            loadingText.setText(mDefaultTextTabLoadingTextUrl);
        } else {
            loadingText.setText(R.string.public_loading_text);
        }
        error.setVisibility(GONE);
        loading.setVisibility(isShowContent ? GONE : VISIBLE);
        getContentView().setVisibility(isShowContent ? VISIBLE : GONE);
    }

    public void loading(boolean isShowContent, boolean shouLoading) {
        if (!TextUtils.isEmpty(mDefaultTextTabLoadingTextUrl)) {
            loadingText.setText(mDefaultTextTabLoadingTextUrl);
        } else {
            loadingText.setText(R.string.public_loading_text);
        }
        loading.setVisibility(GONE);
        error.setVisibility(GONE);
        getContentView().setVisibility(isShowContent ? VISIBLE : GONE);
    }

    public void finish() {
        loading.setVisibility(GONE);
        error.setVisibility(GONE);
        getContentView().setVisibility(VISIBLE);
        mLoadLayout.setPadding(0, 0, 0, 0);
    }

    public void finishError() {
        loading.setVisibility(GONE);
        error.setVisibility(GONE);
    }

    public void finishLayout() {
        loading.setVisibility(GONE);
        error.setVisibility(GONE);
        this.setVisibility(View.GONE);
    }

    public void finishLoad() {
        loading.setVisibility(GONE);
    }

    public void netError(boolean isShowContent) {
        netError(isShowContent, true);
    }

    /**
     * 显示网络有误页面
     * 
     * @param isShowContent
     * @param isErrorTransparent
     */
    public void netError(boolean isShowContent, boolean isErrorTransparent) {
        loading.setVisibility(GONE);
        error.setVisibility(VISIBLE);
        error.setBackgroundColor(isShowContent && isErrorTransparent ? Color.TRANSPARENT : getResources().getColor(
                R.color.letv_base_bg));
        getContentView().setVisibility(isShowContent ? VISIBLE : GONE);
        if (!TextUtils.isEmpty(mDefaultTextTabNoNetTextUrl)) {
            if (mDefaultTextTabNoNetTextUrl.contains("#")) {
                // eg:网络未连接#请检查网络后点击页面刷新
                String[] datas = mDefaultTextTabNoNetTextUrl.split("#");
                if (datas.length == 2) {
                    errorTxt1.setText(datas[0]);
                    errorTxt2.setText(datas[1]);
                    return;
                }
            }
            errorTxt1.setText(mDefaultTextTabNoNetTextUrl);
        } else {
            errorTxt1.setText(mDefaultTextTabNoNetTextUrl);
        }
    }
    
    public void fullNetError(boolean fullNetError){
    	loading.setVisibility(GONE);
    	fullError.setVisibility(VISIBLE);
    	isFullNetError = fullNetError;
    }

    public void dataError(boolean isShowContent) {
        dataError(isShowContent, true);
    }

    /**
     * 显示数据有误页面
     * 
     * @param isShowContent
     * @param isErrorTransparent 错误页面的背景是否透明
     */
    public void dataError(boolean isShowContent, boolean isErrorTransparent) {
        loading.setVisibility(GONE);
        error.setVisibility(VISIBLE);
        errorImage.setImageResource(R.drawable.letv_data_null);
        error.setBackgroundColor(isShowContent && isErrorTransparent ? Color.TRANSPARENT : getResources().getColor(
                R.color.letv_base_bg));
        getContentView().setVisibility(isShowContent ? VISIBLE : GONE);

        if (!TextUtils.isEmpty(mDefaultTextTabNoDataTextUrl)) {
            if (mDefaultTextTabNoDataTextUrl.contains("#")) {
                String[] datas = mDefaultTextTabNoDataTextUrl.split("#");
                if (datas.length == 2) {
                    errorTxt1.setText(datas[0]);
                    errorTxt2.setText(datas[1]);
                    return;
                }
            }
            errorTxt1.setText(mDefaultTextTabNoDataTextUrl);
        } else {
            errorTxt1.setText(R.string.public_loading_datanull);
        }
    }

    public void error(String errMsg) {
        error.setBackgroundColor(getResources().getColor(R.color.letv_color_fff6f6f6));
        loading.setVisibility(GONE);
        errorImage.setVisibility(GONE);
        error.setVisibility(VISIBLE);
        errorTxt1.setText(errMsg);
        getContentView().setVisibility(GONE);
    }
    

    public void showErrorMessage(String errMsg) {
        loading.setVisibility(GONE);
        errorImage.setVisibility(GONE);
        error.setVisibility(VISIBLE);
        error.setBackgroundColor(Color.TRANSPARENT);
        errorTxt1.setText(errMsg);
        getContentView().setVisibility(VISIBLE);
    }

    public void error(int errmsg) {
        loading.setVisibility(GONE);
        errorImage.setVisibility(GONE);
        error.setVisibility(VISIBLE);
        errorTxt1.setText(errmsg);
        errorTxt2.setVisibility(GONE);
        getContentView().setVisibility(GONE);
    }

    public void _error(int errmsg) {
        loading.setVisibility(GONE);
        errorImage.setVisibility(VISIBLE);
        error.setVisibility(VISIBLE);
        errorTxt1.setText(errmsg);
    }

    // 聊天室
    public void chatError(int errmsg) {
        loading.setVisibility(GONE);
        error.setVisibility(VISIBLE);
        errorImage.setImageResource(R.drawable.letv_chat_error_normal);
        errorTxt1.setText(errmsg);
        errorTxt2.setVisibility(GONE);
        error.setBackgroundColor(getResources().getColor(R.color.letv_base_bg));
        getContentView().setVisibility(VISIBLE);
    }

    // 暂无相关视频
    public void dataNull(int errmsg, int errIcon) {
        loading.setVisibility(GONE);
        error.setVisibility(VISIBLE);
        errorImage.setImageResource(errIcon);
        errorTxt1.setText(errmsg);
        errorTxt2.setVisibility(GONE);
        error.setBackgroundColor(getResources().getColor(R.color.letv_base_bg));
        getContentView().setVisibility(VISIBLE);
    }

    public void setErrorBackgroundColor(int color) {
        error.setBackgroundColor(color);
    }

    public TextView getLoadingText() {
        return loadingText;
    }

    public void finishHalfPlay() {
        loading.setVisibility(GONE);
        getContentView().setVisibility(VISIBLE);
    }

    /**
     * 获取错误及loading页的父view
     * 
     * @return
     */
    public View getLoadlayoutView() {
        return mLoadLayout;
    }
    
    public interface RefreshData {

        void refreshData();
    }
}
